package com.vone.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;

import com.vone.util.ImageUtil;
public class MyComboBoxUI extends BasicComboBoxUI{
	private JButton arrow;
	 private boolean boundsLight = false;

	 public MyComboBoxUI() {
	  super();
	 }

	 protected JButton createArrowButton() {
	  arrow = new JButton();
	  arrow.setBorder(null);
	  arrow.setOpaque(true);
	  arrow.setContentAreaFilled(false);
	  arrow.setIcon(ImageUtil.getBtnComboBoxIcon());
	  return arrow;
	 }
	 
	/* public void configureArrowButton(){
	        super.configureArrowButton();
	        if (arrowButton != null) {
	            arrowButton.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
	            arrowButton.setBackground(Color.white);
	    		arrowButton.addMouseListener(new MouseAdapter(){
	    			 public void mouseClicked(MouseEvent e){
	    				 arrowButton.getParent().getParent().repaint();
	    			 }
	    			 public void mousePressed(MouseEvent e){
	    				 arrowButton.getParent().getParent().repaint();
	    			 }
	    			 public void mouseReleased(MouseEvent e){
	    				 arrowButton.getParent().getParent().repaint();
	    			 }
	    			 public void mouseEntered(MouseEvent e){
	    				 arrowButton.getParent().getParent().repaint();
	    			 }
	    			 public void mouseExited(MouseEvent e){
	    				 arrowButton.getParent().getParent().repaint();
	    			 }
	    		});
	    		
	    		arrowButton.addActionListener(new ActionListener(){
	    			@Override
	    			public void actionPerformed(ActionEvent arg0) {
	   				 arrowButton.getParent().getParent().repaint();
	    			}
	    		});
	        }
	 }*/
	 
	 public void paint(Graphics g, JComponent c) {
	  hasFocus = comboBox.hasFocus();
	  Graphics2D g2 = (Graphics2D)g;
	  if (!comboBox.isEditable()) {
	   Rectangle r = rectangleForCurrentValue();
	   //重点:JComboBox的textfield 的绘�?并不是靠Renderer来控�?这点让我很吃�?
	   //它会通过paintCurrentValueBackground来绘制背�?
	   //然后通过paintCurrentValue();去绘制JComboBox里显示的�?
	   paintCurrentValueBackground(g2, r, hasFocus);
	   paintCurrentValue(g2, r, hasFocus);
	  }
	  
	  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	    RenderingHints.VALUE_ANTIALIAS_ON);

	  if (comboBox.isPopupVisible()) {

	//   arrow.setIcon(null);
	  } else {

	//   arrow.setIcon(null);
	  }
	  }

	 public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) {
	  Font oldFont = comboBox.getFont();
	  super.paintCurrentValue(g, bounds, hasFocus);
	  comboBox.setFont(oldFont);
	 }
	 
	 public void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus){
		 g.setColor(Color.white);
		 super.paintCurrentValue(g, bounds, hasFocus);
	 }

	 public Dimension getPreferredSize(JComponent c) {
	  return super.getPreferredSize(c);
	 }

	 public boolean isBoundsLight() {
	  return boundsLight;
	 }

	 public void setBoundsLight(boolean boundsLight) {
	  this.boundsLight = boundsLight;
	 }

	protected ComboPopup createPopup() {
		ComboPopup popup = new BasicComboPopup(comboBox) {
		private static final long serialVersionUID = 1L;
		protected JScrollPane createScroller() {
			MyScrollPane sp = new MyScrollPane(list,
				   ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			sp.setHorizontalScrollBar(null);
			sp.setBackground(Color.white);
			return sp;
	   }
	   //重载paintBorder方法 来画出我们想要的边框..
	   public void paintBorder(Graphics g){
		   Graphics2D g2 = (Graphics2D) g;
		   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				   RenderingHints.VALUE_ANTIALIAS_ON);
		   g2.setColor(new Color(39,157,227));
		   g2.drawRoundRect(0,-arrow.getHeight(),getWidth()-1,getHeight()+arrow.getHeight()-1,0,0);
	   }
	  };
	  return popup;
	 }
}
